package com.example.mapinformationandroid.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.RequestOptions;
import com.example.mapinformationandroid.Model.Bean.InformationBean;
import com.example.mapinformationandroid.Model.Information;
import com.example.mapinformationandroid.R;
import com.example.mapinformationandroid.UI.InformationDetails;

import java.util.List;

public class InformationCardAdapter extends RecyclerView.Adapter<InformationCardAdapter.ViewHolder> {
    private List<Information> informationList;
    private Context context;
    private int positionColor = 1;

    @NonNull
    @Override
    public InformationCardAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.information_card, parent, false);
        ViewHolder holder = new ViewHolder(view);
        holder.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                InformationBean informationBean = new InformationBean( informationList.get(position));
                Bundle bundle = new Bundle();
                Intent intent = new Intent(v.getContext(), InformationDetails.class);
                bundle.putSerializable("information",informationBean);
                intent.putExtra("message",bundle);
                v.getContext().startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull InformationCardAdapter.ViewHolder holder, int position) {

        Information information = informationList.get(position);
        holder.information_title.setText(information.getTitle());
//        holder.back.getResources().getColor(setColor());
        holder.information_place.setText("地址:" + information.getPlace());
        holder.information_time.setText("时间" + information.getCreate_at());
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.defult_information); //添加占位图
        options.error(R.drawable.defult_information)
                .centerCrop();
        Glide.with(context)
                .load(information.getPicture())
                .apply(options)
                .into(holder.information_picture);


    }

    public InformationCardAdapter(List<Information> information, Context context) {
        this.context = context;
        informationList = information;
    }

    @Override
    public int getItemCount() {
        return informationList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView information_picture;
        TextView information_title, information_time, information_place;
        LinearLayout back;

        public ViewHolder(View view) {
            super(view);
            back = view.findViewById(R.id.card_background);
            information_title = view.findViewById(R.id.tv_information_title);
            information_place = view.findViewById(R.id.tv_place);
            information_time = view.findViewById(R.id.tv_time);
            information_picture = view.findViewById(R.id.img_picture);
        }

    }


//    private int setColor() {
//        Integer color = 0;
//        switch (positionColor) {
//            case 1:
//                positionColor += positionColor;
//                color = R.color.colorPrimary;
//                break;
//            case 2:
//                positionColor = 1;
//                color = R.color.colorPrimaryDark;
//                break;
//
//            default:
//                color = Color.MAGENTA;
//        }
//        return color;
//    }
}
